const db = require('../db/index')

// 获取宠物信息列表
exports.petList = (req, res) => {
  const sql = `select * from pet`
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    if (results.length < 0) return res.cc('获取宠物信息列表失败！')
    res.send({
      status: 0,
      message: '获取宠物信息列表成功！',
      data: results
    })
  })
}

// 搜索宠物信息
exports.searchPet = (req, res) => {
  const queryStr = `%${req.query.name}%`
  const sql = `select * from pet where name like ? `
  db.query(sql, queryStr, (err, results) => {
    if (err) return res.cc(err)
    if (results.length < 0) return res.cc('没有您查找的宠物！')
    res.send({
      status: 0,
      message: '查找的宠物成功！',
      data: results
    })
  })
}

// 宠物分类
exports.sortPet = (req, res) => {
  const sql = `select * from pet where sort = ?`
  db.query(sql, req.query.sort, (err, results) => {
    if (err) return res.cc(err)
    if (results.length < 0) return res.cc('没有您查找的宠物！')
    res.send({
      status: 0,
      message: '宠物分类成功！',
      data: results
    })
  })
}

// 获取宠物下单信息
exports.orderPet = (req, res) => {
  const sql = `select * from pet where No = ?`
  db.query(sql, req.query.No, (err, results) => {
    if (err) return res.cc(err)
    if (results.length < 0) return res.cc('获取宠物下单信息失败！')
    res.send({
      status: 0,
      message: '获取宠物下单信息成功！',
      data: results
    })
  })
}
